/*
 * SearchControler.java
 *
 * Created on 7 janvier 2008, 11:06
 */
package com.reimscopie.controlers ;

import javax.naming.* ;

import com.reimscopie.* ;
import com.reimscopie.beans.interfaces.* ;


/**
 * The search controler.
 */
public class SearchControler extends WebControler {
  
  // the user bean
  private ReimsCopieUserRemote bean ;
  
  
  /**
   * Create a new <code>SearchControler</code> instance
   */
  public SearchControler() throws NamingException {
    
    String name ="com.reimscopie.beans.session.ReimsCopieUserBean_" +
        ReimsCopieUserRemote.class.getName()+"@Remote" ;
    
    bean = (ReimsCopieUserRemote)getContext().lookup(name) ;
    
  }
  
  
  // expression
  public String expr ;
  public void      setExpr(String expr)      {this.expr = expr;}
  public String    getExpr()                 {return expr;}
  
  // result
  private Product[] result ;
  public Product[] getResult()               {return result;}
  
  
  /**
   * Perform the search.
   *
   * <p> Navigation keys are:
   * - search-result
   * - error
   */
  public String search() throws Exception {
    
    // no expression
    if (expr == null)
      return null ;
    
    beginTransaction() ;
    
    try {
      long[] ids = bean.search(expr) ;
      result = new Product[ids.length] ;
      for (int i=0;i<ids.length;i++)
        result[i] = bean.getProduct(ids[i]) ;
      
      commitTransaction() ;
      return "search-result" ;
    }
    catch (Exception e) {
      rollbackTransaction() ;
      return "error" ;
    }
    
  }
  
}
